package saucaca.fragment;

import java.util.ArrayList;

import org.json.JSONException;

import saucaca.Adapter.ExpandableRingtoneAdapter;
import saucaca.Adapter.ExpandableRingtoneAdapter2;
import saucaca.Adapter.ExpandableSongAdapter2;
import saucaca.CustomView.CustomExpandableListView;
import saucaca.CustomView.ProgressBarCircularIndeterminate;
import saucaca.JSON.JsonReader;
import saucaca.JSON.WServiceLib;
import saucaca.Object.RingtoneObject;
import saucaca.Object.SongObject;
import saucaca.Util.CMD;
import saucaca.fragment.OnlineSongList2.GetFromServices;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.AbsListView.OnScrollListener;

import com.lemon.kenhnhacvn.BaseEngine;
import com.lemon.kenhnhacvn.MainActivity;
import com.lemon.kenhnhacvn.R;


public class RingToneListSong extends Fragment{
	public static final String TAG = RingToneListSong.class.getSimpleName();
	ArrayList<RingtoneObject> ringtones;
	MainActivity mActivity;
	private View mView_EmptyLoading = null;
	private ProgressBar progressBar;
	ImageView imgEmpty, imgError;
	TextView txtStatus;
	boolean isFromMenu;
	public MediaPlayer mediaPlayer;
	
	private ExpandableListView expandListview;
	ProgressBarCircularIndeterminate progressBarFooter;
	ExpandableRingtoneAdapter2 adapter;
	int m_PreviousTotalCount = 0;
	
	RingtoneObject ringtoneObject;
	boolean checkString(String str) {
		if (str != null && !str.equals("")) {
			return true;
		} else
			return false;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		mActivity = (MainActivity) getActivity();
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
		try {
			mediaPlayer.pause();
			mediaPlayer.reset();
			mediaPlayer.release();
			mediaPlayer = null;
		} catch (Exception e) {
		}
	}

	@Override
	public void onDetach() {
		super.onDetach();
		try {
			mediaPlayer.pause();
			mediaPlayer.reset();
			mediaPlayer.release();
			mediaPlayer = null;
		} catch (Exception e) {
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View android = inflater.inflate(R.layout.listview_expand2, container, false);
		findviewByID(android);
		Bundle bundle = getArguments();
		ringtoneObject = (RingtoneObject) bundle.getSerializable(MainActivity.ID);
		new GetFromServices().execute(ringtoneObject.getSONG_ID());
		mActivity.myActionBar.setMenu(ringtoneObject.getTXNAME(), false, false);
		return android;
	}
	@Override
	public void onHiddenChanged(boolean hidden) {
		super.onHiddenChanged(hidden);
		if(!hidden)
			mActivity.myActionBar.setMenu(ringtoneObject.getTXNAME(), false, false);
	}

	@SuppressLint("InlinedApi")
	private void findviewByID(View view) {
		expandListview = (ExpandableListView) view.findViewById(R.id.layoutlistview_ExpandListview);
		progressBarFooter = (ProgressBarCircularIndeterminate) view.findViewById(R.id.progressBarloadmore);
		mView_EmptyLoading = view.findViewById(R.id.empty_loading);
		expandListview.setEmptyView(mView_EmptyLoading);
		progressBar = (ProgressBar) mView_EmptyLoading.findViewById(R.id.empty_loading_progress);
		imgEmpty = (ImageView) mView_EmptyLoading.findViewById(R.id.empty_loading_NoItem);
		imgError = (ImageView) mView_EmptyLoading.findViewById(R.id.empty_loading_Error);
		txtStatus = (TextView) mView_EmptyLoading.findViewById(R.id.empty_loading_text);
	}

	class GetFromServices extends AsyncTask<String, String, String> {
		@Override
		protected String doInBackground(String... params) {
			String str = WServiceLib.idDetais(CMD.CMD_TONE_DETAIL, params[0]);
//			 BaseEngine.showLogformLongString(str);
			return str;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			if (result != null)
				try {
					ArrayList<RingtoneObject> listRingtoneObjects = JsonReader.readRingToneDetails(result);
					adapter=new ExpandableRingtoneAdapter2(RingToneListSong.this, expandListview, listRingtoneObjects);
					expandListview.setAdapter(adapter);
					adapter.expandGroup(0);
					if (listRingtoneObjects.size() == 0) {
						progressBar.setVisibility(View.GONE);
						imgEmpty.setVisibility(View.VISIBLE);
						txtStatus.setText(mActivity.getResources().getString(R.string.no_item_song));
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
		}
	}
}
